/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import model.UserModel;
import util.DataUtil;

/**
 *
 * @author ThanhToan
 */
public class UserDAO {
    public static boolean insertUser(UserModel userModel){
        boolean result = false;
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_InsertUser(?,?,?,?,?,?,?,?)}");
            csmt.setString("Username", userModel.getUsername());
            csmt.setString("Password", userModel.getPassword());
            csmt.setString("Phone", userModel.getPhone());
            csmt.setNString("Firstname", userModel.getFirstname());
            csmt.setNString("Lastname", userModel.getLastname());
            csmt.setNString("Address", userModel.getAddress());
            csmt.setString("Email", userModel.getEmail());
            csmt.setString("Birthday", userModel.getYear() + "-" + userModel.getMonth() + "-" + userModel.getDay());

            result = DataUtil.executeNonStore(csmt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean updateUser(UserModel userModel){
        boolean result = false;
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_InsertUser(?,?,?,?,?,?,?,?)}");
            csmt.setString("id", userModel.getUserID());
            csmt.setString("phone", userModel.getPhone());
            csmt.setNString("firstname", userModel.getFirstname());
            csmt.setNString("lastname", userModel.getLastname());
            csmt.setNString("address", userModel.getAddress());
            csmt.setString("email", userModel.getEmail());
            csmt.setString("accountType", userModel.getAccountType());
            csmt.setString("birthday", userModel.getYear() + "-" + userModel.getMonth() + "-" + userModel.getDay());
            result = DataUtil.executeNonStore(csmt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static ArrayList<UserModel> findAllUser(){
        ArrayList<UserModel> userList = new ArrayList<UserModel>();
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_FindAllUser()}");
            ResultSet rs = DataUtil.executeStore(csmt);
            while(rs.next()){
                UserModel userModel = new UserModel();
                userModel.setUserID(rs.getString("ID"));
                userModel.setUsername(rs.getString("Username"));
                userModel.setPassword(rs.getString("Password"));
                userModel.setSalt(rs.getString("Salt"));
                userModel.setAccountType(rs.getString("Name"));
                userModel.setFirstname(rs.getString("FirstName"));
                userModel.setLastname(rs.getString("LastName"));
                userModel.setEmail(rs.getString("Email"));
                userModel.setAddress(rs.getString("Address"));
                userModel.setPhone(rs.getString("Phone"));
                userModel.setMonth(rs.getString("BMonth"));
                userModel.setDay(rs.getString("BDay"));
                userModel.setYear(rs.getString("BYear"));
                userList.add(userModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }

    public static UserModel getUserByUserID(String id){
        UserModel userModel = new UserModel();
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetUserByID(?)}");
            csmt.setString(id, "id");
            ResultSet rs = DataUtil.executeStore(csmt);
            if(rs.next()){
                userModel.setUserID(rs.getString("ID"));
                userModel.setUsername(rs.getString("Username"));
                userModel.setPassword(rs.getString("Password"));
                userModel.setSalt(rs.getString("Salt"));
                userModel.setAccountType(rs.getString("AccountType"));
                userModel.setFirstname(rs.getString("FirstName"));
                userModel.setLastname(rs.getString("LastName"));
                userModel.setEmail(rs.getString("Email"));
                userModel.setAddress(rs.getString("Address"));
                userModel.setPhone(rs.getString("Phone"));
                userModel.setMonth(rs.getString("BMonth"));
                userModel.setDay(rs.getString("BDay"));
                userModel.setYear(rs.getString("BYear"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userModel;
    }

    public static ArrayList<UserModel> getUserByRole(String role){
        ArrayList<UserModel> userList = new ArrayList<UserModel>();
        try {
            CallableStatement csmt = DataUtil.getConnection().prepareCall("{call sp_GetUserByRole(?)}");
            csmt.setString(role, "role");
            ResultSet rs = DataUtil.executeStore(csmt);
            while(rs.next()){
                UserModel userModel = new UserModel();
                userModel.setUserID(rs.getString("ID"));
                userModel.setUsername(rs.getString("Username"));
                userModel.setPassword(rs.getString("Password"));
                userModel.setSalt(rs.getString("Salt"));
                userModel.setAccountType(rs.getString("AccountType"));
                userModel.setFirstname(rs.getString("FirstName"));
                userModel.setLastname(rs.getString("LastName"));
                userModel.setEmail(rs.getString("Email"));
                userModel.setAddress(rs.getString("Address"));
                userModel.setPhone(rs.getString("Phone"));
                userModel.setMonth(rs.getString("BMonth"));
                userModel.setDay(rs.getString("BDay"));
                userModel.setYear(rs.getString("BYear"));
                userList.add(userModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }
}
